package com.zrf.gulimall.product.app;

import com.zrf.gulimall.common.database.utils.PageUtils;
import com.zrf.gulimall.commnon.core.utils.R;
import com.zrf.gulimall.product.entity.ProductAttrValueEntity;
import com.zrf.gulimall.product.service.AttrService;
import com.zrf.gulimall.product.service.ProductAttrValueService;
import com.zrf.gulimall.product.vo.AttrVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author zrf
 * @email 120662332@qq.com
 * @date 2021-01-23 16:07:19
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
  @Resource
  private AttrService attrService;
  @Resource
  private ProductAttrValueService productAttrValueService;

  /**
   * 列表
   */
  @RequestMapping("/{attrType}/list/{catelogId}")
  //@RequiresPermissions("product:attr:list")
  public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId,
                @PathVariable("attrType") String attrType) {
    PageUtils page = attrService.queryPage(params, catelogId, attrType);

    return R.ok().put("page", page);
  }

  /**
   * 列表
   */
  @GetMapping("/base/spu/{spuId}")
  //@RequiresPermissions("product:attr:list")
  public R listForSpu(@PathVariable("spuId") Long spuId) {
    List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.queryBySpuId(spuId);
    return R.ok().put("data", productAttrValueEntityList);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{attrId}")
  //@RequiresPermissions("product:attr:info")
  public R info(@PathVariable("attrId") Long attrId) {
    AttrVo attrVo = attrService.queryRelationInfoById(attrId);
    return R.ok().put("attr", attrVo);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("product:attr:save")
  public R save(@RequestBody AttrVo attr) {
    attrService.saveAttr(attr);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("product:attr:update")
  public R update(@RequestBody AttrVo attr) {
    attrService.update(attr);
    return R.ok();
  }
  /**
   * 修改
   */
  @RequestMapping("/update/{spuId}")
  //@RequiresPermissions("product:attr:update")
  public R update(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> productAttrValueEntities) {

    productAttrValueService.updateAttrValue(spuId, productAttrValueEntities);
    return R.ok();
  }


  /**
   * 删除
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("product:attr:delete")
  public R delete(@RequestBody Long[] attrIds) {
    attrService.removeByIds(Arrays.asList(attrIds));

    return R.ok();
  }

}
